﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.ComponentModel;

namespace Hangout.Client.Controls.Notifications
{
    public partial class NotificationList : UserControl, INotifyPropertyChanged
    {

        public delegate void IdHelper(int id);

        public event IdHelper NavigateToHangout;

        public event IdHelper NavigateToProfile;

        private List<NotificationService.Notification> notifications;

        public List<NotificationService.Notification> Notifications
        {
            get
            {
                try
                    {
                        if (notifications == null)
                        {
                            notifications = new List<NotificationService.Notification>();
                            NoLBL.Visibility = System.Windows.Visibility.Visible;

                        }
                        return notifications;
                    }
                    catch (Exception ex)
                    {
                        Core.Exceptions.ExceptionReporting.Report(ex);
                        MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                        return new List<NotificationService.Notification>();
                    }

            }
        
            set
            {
                try
                {
                    notifications = value;

                    RefreshList();

                    FirePropertyChanged("Notifications");
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }
                
            }
        }

        public void RefreshList()
        {
            try
            {
                if (notifications == null)
                {
                    notifications = new List<NotificationService.Notification>();
                }
                if (notifications.Count > 0)
                {

                    NotificationsLB.ItemsSource = null;
                    notifications = notifications.OrderByDescending(o => o.DatetimePosted).ToList();
                    NotificationsLB.DataContext = Notifications;
                    NotificationsLB.Visibility = System.Windows.Visibility.Visible;
                    NotificationsLB.ItemsSource = Notifications;
                    NoLBL.Visibility = System.Windows.Visibility.Collapsed;

                }
                else
                {
                    NotificationsLB.ItemsSource = null;
                    NoLBL.Visibility = System.Windows.Visibility.Visible;
                    NotificationsLB.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }



        public int take = 10; //always take only 10 notifications at a time from the server

        public int skip = 0; //skip 0 notifications at first.

        public NotificationList()
        {
            InitializeComponent();
        }

       

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion


        protected void FirePropertyChanged(string propertyName)
        {
            try
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void NotificationSelected(NotificationService.Notification notification)
        {
            try
            {
                string meta = notification.MetaData;
                //Meta Data
                if (meta != "")
                {
                    //check the meta data and navigate accordingly. :)

                    if (meta.Contains("User"))
                    {
                        int id = Convert.ToInt32(meta.Split(':')[1]);
                        if (NavigateToProfile != null)
                        {
                            NavigateToProfile(id);
                        }
                    }

                    if (meta.Contains("Hangout"))
                    {
                        int id = Convert.ToInt32(meta.Split(':')[1]);
                        if (NavigateToHangout != null)
                        {
                            NavigateToHangout(id);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void NotificationsLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (NotificationsLB.SelectedItem != null)
                {
                    NotificationSelected((NotificationService.Notification)NotificationsLB.SelectedItem);
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        


    }
}
